package com.ai.plug.core.parser.tool.starter;

import com.ai.plug.core.parser.tool.des.AbstractDesParser;
import com.ai.plug.core.parser.tool.param.AbstractParamParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

/**
 *早期解析器的使用，对收集到的参数进行拼装形成元数据（断路解析 ，有值就直接返回了）
 */
@Slf4j
public final class SingleStarter extends AbstractStarter {


    @Override
    public String runDesParse(List<AbstractDesParser> parserList, Method method, Class<?> toolClass) {
        //提取类中方法的信息获取name和描述
        String result = parserList
                .stream()
                .map((parser) -> parser.doDesParse(method, toolClass))
                .filter(StringUtils::hasText)
                .findFirst()
                .orElse(null);

        if (!StringUtils.hasText(result)) {
            log.warn("Please note that the parsing module you specified resulted in the {} tool not parsing the tool description", toolClass.getName() + "-" + method.getName());
            return AbstractDesParser.doDefaultParse(method, toolClass);
        }
        return result;

    }

    @Override
    public Boolean runParamRequiredParse(List<AbstractParamParser> parserList, Method method, Class<?> toolClass, int index) {

        Boolean result = parserList
                .stream()
                .map((parser) -> parser.doParamRequiredParse(method, toolClass, index))
                .filter(Objects::nonNull)
                .findFirst()
                .orElse(null);

        if (result == null) {
            log.warn("Please note that the parsing module you specified resulted in the {} tool not parsing the param required", toolClass.getName() + "-" + method.getName());
            return AbstractParamParser.doDefaultParamRequiredParse(method, toolClass, index);
        }
        return result;
    }

    @Override
    public String runParamDesParse(List<AbstractParamParser> parserList, Method method, Class<?> toolClass, int index) {
        String result = parserList
                .stream()
                .map((parser) -> parser.doParamDesParse(method, toolClass, index))
                .filter(StringUtils::hasText)
                .findFirst()
                .orElse(null);

        if (result == null) {
            log.warn("Please note that the parsing module you specified resulted in the {} tool not parsing the param required", toolClass.getName() + "-" + method.getName());
            return AbstractParamParser.doDefaultParamDesParse(method, toolClass, index);
        }

        return result;

    }


}
